/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 * aion-unique is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-unique is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.questEngine.handlers.template;

import com.aionengine.gameserver.model.DialogAction;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.skill.PlayerSkillEntry;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SKILL_LIST;
import com.aionengine.gameserver.questEngine.handlers.QuestHandler;
import com.aionengine.gameserver.questEngine.model.QuestEnv;
import com.aionengine.gameserver.questEngine.model.QuestState;
import com.aionengine.gameserver.questEngine.model.QuestStatus;
import com.aionengine.gameserver.services.craft.CraftSkillUpdateService;
import com.aionengine.gameserver.utils.PacketSendUtility;

import java.util.HashSet;

/**
 * @author Bobobear
 */
public class CraftingRewards extends QuestHandler {

    private final int startNpcId;
    private final int skillId;
    private final int levelReward;
    private final int questMovie;
    private final int endNpcId;

    public CraftingRewards(int questId, int startNpcId, int skillId, int levelReward, int endNpcId, int questMovie) {
        super(questId);
        this.startNpcId = startNpcId;
        this.skillId = skillId;
        this.levelReward = levelReward;
        if (endNpcId != 0) {
            this.endNpcId = endNpcId;
        } else {
            this.endNpcId = startNpcId;
        }
        this.questMovie = questMovie;
    }

    @Override
    public void register() {
        qe.registerQuestNpc(startNpcId).addOnQuestStart(questId);
        qe.registerQuestNpc(startNpcId).addOnTalkEvent(questId);
        if (questMovie != 0) {
            qe.registerOnMovieEndQuest(questMovie, questId);
        }
        if (endNpcId != startNpcId) {
            qe.registerQuestNpc(endNpcId).addOnTalkEvent(questId);
        }
    }

    @Override
    public boolean onDialogEvent(QuestEnv env) {
        Player player = env.getPlayer();
        QuestState qs = player.getQuestStateList().getQuestState(questId);
        DialogAction dialog = env.getDialog();
        int targetId = env.getTargetId();
        PlayerSkillEntry skill = player.getSkillList().getSkillEntry(skillId);

        if (skill != null) {
            int playerSkillLevel = skill.getSkillLevel();
            if (!canLearn(player) && playerSkillLevel != levelReward) {
                return false;
            }
        }

        if (qs == null || qs.getStatus() == QuestStatus.NONE) {
            if (targetId == startNpcId) {
                switch (dialog) {
                    case QUEST_SELECT: {
                        return sendQuestDialog(env, 1011);
                    }
                    default: {
                        return sendQuestStartDialog(env);
                    }
                }
            }
        } else if (qs.getStatus() == QuestStatus.START) {
            if (targetId == endNpcId) {
                switch (dialog) {
                    case QUEST_SELECT: {
                        return sendQuestDialog(env, 2375);
                    }
                    case SELECT_QUEST_REWARD: {
                        qs.setQuestVar(0);
                        qs.setStatus(QuestStatus.REWARD);
                        updateQuestStatus(env);
                        if (questMovie != 0) {
                            playQuestMovie(env, questMovie);
                        } else {
                            player.getSkillList().addSkill(player, skillId, levelReward);
                        }
                        return sendQuestEndDialog(env);
                    }
				default:
					break;
                }
            }
        } else if (qs.getStatus() == QuestStatus.REWARD) {
            if (targetId == endNpcId) {
                switch (dialog) {
                    case QUEST_SELECT: {
                        return sendQuestEndDialog(env);
                    }
                    default: {
                        return sendQuestEndDialog(env);
                    }
                }
            }
        }
        return false;
    }

    private boolean canLearn(Player player) {
        return levelReward == 400 ? CraftSkillUpdateService.canLearnMoreExpertCraftingSkill(player)
                : levelReward == 500 ? CraftSkillUpdateService.canLearnMoreMasterCraftingSkill(player) : true;
    }

    @Override
    public boolean onMovieEndEvent(QuestEnv env, int movieId) {
        Player player = env.getPlayer();
        QuestState qs = player.getQuestStateList().getQuestState(questId);
        if (qs.getStatus() == QuestStatus.REWARD) {
            if (movieId == questMovie && canLearn(player)) {
                player.getSkillList().addSkill(player, skillId, levelReward);
                player.getRecipeList().autoLearnRecipe(player, skillId, levelReward);
                PacketSendUtility.sendPacket(player, new SM_SKILL_LIST(player.getSkillList().getSkillEntry(skillId), 1330064, false));
                return true;
            }
        }
        return false;
    }

    @Override
    public HashSet<Integer> getNpcIds() {
        if (constantSpawns == null) {
            constantSpawns = new HashSet<>();
            constantSpawns.add(startNpcId);
            if (endNpcId != 0)
                constantSpawns.add(endNpcId);
        }
        return constantSpawns;
    }
}
